//
//  OtherDetailFacilityCell.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/24.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class OtherDetailFacilityCell: UITableViewCell {

    @IBOutlet weak var facilitiesDesLabel: UILabel!
    @IBOutlet weak var collectionview: UICollectionView!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var collectionViewH: NSLayoutConstraint!
    
    private let width = (kScreenWidth - 60 - 16 * 3) / 4
    
    private let height = 50
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        facilitiesDesLabel.text = LocalizedString("hotel_facilities")
        
        layout.itemSize = CGSize(width: width, height: height)
        
        layout.minimumLineSpacing = 16
        layout.minimumInteritemSpacing = 16
        
        layout.sectionInset = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)
        
        collectionViewH.constant = CGFloat(height * 3 + 60 + 16 * 2)
        
        collectionview.register(UINib(nibName: "OtherDetailFacilityCCell", bundle: nil), forCellWithReuseIdentifier: "OtherDetailFacilityCCell")
    }
    
    var facilities:[[String:String]]? {
        
        didSet {
            
            self.collectionview.reloadData()
            
            var collectionH:CGFloat = 0
            
            if let count = facilities?.count {
                
                let lines = ceil(CGFloat(count) / 4.0)
                
                collectionH = lines * CGFloat(height) + 60 + (lines - 1) * 16
            }
            
            self.collectionViewH.constant = collectionH
        }
    }
    
}

extension OtherDetailFacilityCell: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return facilities?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let info = facilities?[indexPath.row]
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OtherDetailFacilityCCell", for: indexPath) as! OtherDetailFacilityCCell
        cell.info = info
        
        return cell
    }
}
